package com.yyj.web.controller;

import com.yyj.common.BaseResponse;
import com.yyj.service.RoomService;
import com.yyj.util.RspUtils;
import com.yyj.util.poi.POIUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

@RestController
public class RoomController extends BaseController {

    @Autowired
    RoomService roomService;

    @ApiOperation(value = "查看房号列表")
    @RequestMapping(value = "/auth/rooms", method = RequestMethod.GET)
    public BaseResponse listRoom() {
        try {
            return RspUtils.success(roomService.listRooms());
        } catch (Exception e) {
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "初始化房号列表")
    @RequestMapping(value = "/auth/initRooms", method = RequestMethod.GET)
    public BaseResponse initRoom() {
        String userIdsFile = "room.xlsx";
        try {
            StringBuffer userIdBuffer = new StringBuffer();
            StringBuffer orderNumBuffer = new StringBuffer();
            File file1 = new File(userIdsFile);
            List<String[]> list1 = POIUtil.readExcel(file1);
            list1 = list1.subList(0, 582);
            if (list1.size() > 0) {
                List<String[]> userIdList = list1;
                for (int i = 0; i < userIdList.size(); i++) {
                    String roomNum = userIdList.get(i)[0].trim();
                    String square = userIdList.get(i)[1].trim();
                    roomService.addRoom(roomNum , new BigDecimal(square));
                }
            }
        } catch (Exception e) {

        }
        return RspUtils.success();
    }

}
